<?php

namespace app\controllers;

use yii\log\FileTarget;
use yii\web\Controller;

class ErrorController extends Controller
{

    public function actionError()
    {

        $exception = \Yii::$app->errorHandler->exception;

        $error = "";
        if ($exception) {
            $file = $exception->getFile();
            $line = $exception->getLine();
            $code = $exception->getCode();
            $message = $exception->getMessage();

            $error = "{file: $file, line: $line, code: $line, message: $message, url: $_SERVER[REQUEST_URI], post_data:"
                . http_build_query($_POST) . "}";

            $fileTarget = new FileTarget();
            $fileTarget->logFile = \Yii::$app->getRuntimePath() . "/logs/error.log";
            $fileTarget->messages[] = [
                $error,
                1,
                "application",
                microtime(true)
            ];
            $fileTarget->export();

            // todo 写入数据库
        }


        $this->layout = false;
        return $this->render("error", [
            'err_msg' => $error
        ]);
    }

}
